<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="产品编号" prop="productionNum">
      <el-input v-model="dataForm.productionNum" placeholder="产品编号"></el-input>
    </el-form-item>
    <el-form-item label="表示顺" prop="dispalyOrder">
      <el-input v-model="dataForm.dispalyOrder" placeholder="表示顺"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="productionCount">
      <el-input v-model="dataForm.productionCount" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="总价" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="总价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderNum: 0,
          productionNum: '',
          dispalyOrder: '',
          unitPrice: '',
          productionCount: '',
          totalPrice: ''
        },
        dataRule: {
          productionNum: [
            { required: true, message: '产品编号不能为空', trigger: 'blur' }
          ],
          dispalyOrder: [
            { required: true, message: '表示顺不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          productionCount: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '总价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderNum = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderNum) {
            this.$http({
              url: this.$http.adornUrl(`/order/mesordermangementdetailo/info/${this.dataForm.orderNum}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productionNum = data.mesordermangementdetailo.productionNum
                this.dataForm.dispalyOrder = data.mesordermangementdetailo.dispalyOrder
                this.dataForm.unitPrice = data.mesordermangementdetailo.unitPrice
                this.dataForm.productionCount = data.mesordermangementdetailo.productionCount
                this.dataForm.totalPrice = data.mesordermangementdetailo.totalPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/mesordermangementdetailo/${!this.dataForm.orderNum ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderNum': this.dataForm.orderNum || undefined,
                'productionNum': this.dataForm.productionNum,
                'dispalyOrder': this.dataForm.dispalyOrder,
                'unitPrice': this.dataForm.unitPrice,
                'productionCount': this.dataForm.productionCount,
                'totalPrice': this.dataForm.totalPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
